/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.IdentityBy;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Identity;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.qa.unittest.prov.Names;
/*     */ import com.zimbra.soap.admin.type.CacheEntryType;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLdapProvIdentity
/*     */   extends LdapTest
/*     */ {
/*     */   private static LdapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   private static Domain domain;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  51 */     provUtil = new LdapProvTestUtil();
/*  52 */     prov = provUtil.getProv();
/*  53 */     domain = provUtil.createDomain(baseDomainName());
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  58 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */   private Account createAccount(String localPart) throws Exception {
/*  62 */     return createAccount(localPart, null);
/*     */   }
/*     */   
/*     */   private Account createAccount(String localPart, Map<String, Object> attrs) throws Exception {
/*  66 */     return provUtil.createAccount(localPart, domain, attrs);
/*     */   }
/*     */   
/*     */   private void deleteAccount(Account acct) throws Exception {
/*  70 */     provUtil.deleteAccount(acct);
/*     */   }
/*     */   
/*     */   private void deleteIdentity(Account acct, Identity identity) throws Exception {
/*  74 */     String identityId = identity.getId();
/*  75 */     String identityName = identity.getName();
/*  76 */     prov.deleteIdentity(acct, identityName);
/*  77 */     identity = prov.get(acct, Key.IdentityBy.id, identityId);
/*  78 */     Assert.assertNull(identity);
/*     */   }
/*     */   
/*     */   private Identity createIdentityRaw(Account acct, String identityName) throws Exception {
/*  82 */     Map<String, Object> attrs = new HashMap();
/*     */     
/*  84 */     return prov.createIdentity(acct, identityName, attrs);
/*     */   }
/*     */   
/*     */   private Identity createIdentity(Account acct, String identityName) throws Exception {
/*  88 */     Identity identity = prov.get(acct, Key.IdentityBy.name, identityName);
/*  89 */     Assert.assertNull(identity);
/*     */     
/*  91 */     createIdentityRaw(acct, identityName);
/*     */     
/*  93 */     identity = prov.get(acct, Key.IdentityBy.name, identityName);
/*  94 */     Assert.assertNotNull(identity);
/*  95 */     Assert.assertEquals(identityName, identity.getName());
/*     */     
/*  97 */     return identity;
/*     */   }
/*     */   
/*     */   private Account getFresh(Account acct) throws Exception {
/* 101 */     prov.flushCache(CacheEntryType.account, null);
/* 102 */     return prov.get(Key.AccountBy.id, acct.getId());
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void createIdentity() throws Exception {
/* 107 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart());
/* 108 */     String IDENTITY_NAME = Names.makeIdentityName(genIdentityName());
/*     */     
/* 110 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/* 111 */     Identity identity = createIdentity(acct, IDENTITY_NAME);
/*     */     
/* 113 */     Assert.assertEquals(acct.getId(), identity.getAccount().getId());
/*     */     
/* 115 */     deleteIdentity(acct, identity);
/* 116 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void createIdentityAlreadyExists() throws Exception {
/* 121 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart());
/* 122 */     String IDENTITY_NAME = Names.makeIdentityName(genIdentityName());
/*     */     
/* 124 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/* 125 */     Identity identity = createIdentity(acct, IDENTITY_NAME);
/*     */     
/* 127 */     boolean caughtException = false;
/*     */     try {
/* 129 */       createIdentityRaw(acct, IDENTITY_NAME);
/*     */     } catch (AccountServiceException e) {
/* 131 */       if ("account.IDENTITY_EXISTS".equals(e.getCode())) {
/* 132 */         caughtException = true;
/*     */       }
/*     */     }
/* 135 */     Assert.assertTrue(caughtException);
/*     */     
/* 137 */     deleteIdentity(acct, identity);
/* 138 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void modifyIdentity() throws Exception {
/* 143 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart());
/* 144 */     String IDENTITYE_NAME = Names.makeIdentityName(genIdentityName());
/*     */     
/* 146 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/* 147 */     Identity identity = createIdentity(acct, IDENTITYE_NAME);
/*     */     
/* 149 */     Map<String, Object> attrs = new HashMap();
/* 150 */     String MODIFIED_ATTR_NAME = "zimbraPrefFromDisplay";
/* 151 */     String MODIFIED_ATTR_VALUE = "modifyIdentity";
/* 152 */     attrs.put(MODIFIED_ATTR_NAME, MODIFIED_ATTR_VALUE);
/* 153 */     prov.modifyIdentity(acct, identity.getName(), attrs);
/*     */     
/* 155 */     acct = getFresh(acct);
/* 156 */     identity = prov.get(acct, Key.IdentityBy.name, IDENTITYE_NAME);
/* 157 */     Assert.assertEquals(MODIFIED_ATTR_VALUE, identity.getAttr(MODIFIED_ATTR_NAME));
/*     */     
/* 159 */     deleteIdentity(acct, identity);
/* 160 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void renameIdentity() throws Exception {
/* 165 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart());
/* 166 */     String IDENTITYE_NAME = Names.makeIdentityName(genIdentityName());
/*     */     
/* 168 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/* 169 */     Identity identity = createIdentity(acct, IDENTITYE_NAME);
/*     */     
/* 171 */     Map<String, Object> attrs = new HashMap();
/*     */     
/* 173 */     String MODIFIED_ATTR_NAME = "zimbraPrefIdentityName";
/* 174 */     String NEW_IDENTITY_NAME = genIdentityName("new");
/* 175 */     String MODIFIED_ATTR_VALUE = NEW_IDENTITY_NAME;
/* 176 */     attrs.put(MODIFIED_ATTR_NAME, MODIFIED_ATTR_VALUE);
/* 177 */     prov.modifyIdentity(acct, identity.getName(), attrs);
/*     */     
/* 179 */     acct = getFresh(acct);
/* 180 */     identity = prov.get(acct, Key.IdentityBy.name, NEW_IDENTITY_NAME);
/* 181 */     Assert.assertEquals(MODIFIED_ATTR_VALUE, identity.getAttr(MODIFIED_ATTR_NAME));
/*     */     
/* 183 */     deleteIdentity(acct, identity);
/* 184 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void getAllIdentitys() throws Exception {
/* 189 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart());
/* 190 */     String IDENTITYE_NAME_1 = Names.makeIdentityName(genIdentityName("1"));
/* 191 */     String IDENTITYE_NAME_2 = Names.makeIdentityName(genIdentityName("2"));
/* 192 */     String IDENTITYE_NAME_3 = Names.makeIdentityName(genIdentityName("3"));
/*     */     
/* 194 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/* 195 */     Identity identity1 = createIdentity(acct, IDENTITYE_NAME_1);
/* 196 */     Identity identity2 = createIdentity(acct, IDENTITYE_NAME_2);
/* 197 */     Identity identity3 = createIdentity(acct, IDENTITYE_NAME_3);
/*     */     
/* 199 */     acct = getFresh(acct);
/* 200 */     List<Identity> allIdentitys = prov.getAllIdentities(acct);
/* 201 */     Assert.assertEquals(4L, allIdentitys.size());
/*     */     
/* 203 */     Set<String> allIdentityIds = new HashSet();
/* 204 */     for (Identity identity : allIdentitys) {
/* 205 */       allIdentityIds.add(identity.getId());
/*     */     }
/*     */     
/* 208 */     Assert.assertTrue(allIdentityIds.contains(identity1.getId()));
/* 209 */     Assert.assertTrue(allIdentityIds.contains(identity2.getId()));
/* 210 */     Assert.assertTrue(allIdentityIds.contains(identity3.getId()));
/*     */     
/* 212 */     deleteIdentity(acct, identity1);
/* 213 */     deleteIdentity(acct, identity2);
/* 214 */     deleteIdentity(acct, identity3);
/* 215 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void getIdentity() throws Exception {
/* 220 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart());
/* 221 */     String IDENTITYE_NAME = Names.makeIdentityName(genIdentityName());
/*     */     
/* 223 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/* 224 */     Identity identity = createIdentity(acct, IDENTITYE_NAME);
/* 225 */     String identityId = identity.getId();
/*     */     
/* 227 */     acct = getFresh(acct);
/* 228 */     identity = prov.get(acct, Key.IdentityBy.id, identityId);
/* 229 */     Assert.assertNotNull(identity);
/*     */     
/* 231 */     acct = getFresh(acct);
/* 232 */     identity = prov.get(acct, Key.IdentityBy.name, IDENTITYE_NAME);
/* 233 */     Assert.assertNotNull(identity);
/*     */     
/* 235 */     deleteIdentity(acct, identity);
/* 236 */     deleteAccount(acct);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestLdapProvIdentity.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */